﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using ECS.DAL.Entity;

namespace ECS.DAL.Component
{
    public class UserAuthenticateComponent : BaseSQLModel
    {
        #region private properties
        private const string SQL_PROCEDURE = "usp_AuthenticateUser ";
        private const string SQL_PROCEDURE2 = "usp_SecManLogout";
        #endregion

        #region public mehods
        /// <summary>
        /// get user information by authentication
        /// </summary>
        /// <returns></returns>
        public UserAuthenticate GetUserAuthentication(string userId, string password, string appId, string device, string clearUserFlag, string appVersion)
        {
            DataSet ds = new DataSet();
            UserAuthenticate user = null;
            using (DAL.Entities entities = new Entities("con"))
            {
                entities.Open();
                DbCommand cmd = entities.CreateCommand(SQL_PROCEDURE);
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.Parameters.Add(entities.CreateParameter("@in_vchUserID", userId));
                cmd.Parameters.Add(entities.CreateParameter("@in_vchPassword", password));
                cmd.Parameters.Add(entities.CreateParameter("@in_vchApplicationID", appId));
                cmd.Parameters.Add(entities.CreateParameter("@in_vchDevice", device));
                cmd.Parameters.Add(entities.CreateParameter("@in_vchClearUserFlag", clearUserFlag));
                cmd.Parameters.Add(entities.CreateParameter("@in_vchInterfaceVersionNumber", appVersion));
                DbDataAdapter adapter = entities.CreateDataAdapter(cmd);
                adapter.Fill(ds);
                //DbDataReader reader = cmd.ExecuteReader();
                //if (reader.Read())
                if(ds.Tables[0].Rows.Count > 0)
                {
                    //@out_vchStatus,@out_vchErrorCode,@out_vchMessage, @out_intLocale, 
			        //@out_vchApplicationName, @out_vchMenuID,@out_vchDBServerName, @out_vchDBName,
			        //@out_vchDBUser,	@out_vchDBPassword,	@out_vchDBConnectionString

                    user = new UserAuthenticate();
                    user.UserID = userId;
                    user.UserName = userId;
                    
                    user.Status = ds.Tables[0].Rows[0][0].ToString();
                    user.ErrorCode = ds.Tables[0].Rows[0][1].ToString();
                    user.Message = ds.Tables[0].Rows[0][2].ToString();
                    if (user.Status.Equals("PASS"))
                    {
                        user.Locale = Convert.ToInt32(ds.Tables[0].Rows[0][3]);
                        user.ApplicationName = ds.Tables[0].Rows[0][4].ToString();
                        user.MenuId = ds.Tables[0].Rows[0][5].ToString();
                        user.ServerName = ds.Tables[0].Rows[0][6].ToString();
                        user.DbName = ds.Tables[0].Rows[0][7].ToString();
                        user.DbUser = ds.Tables[0].Rows[0][8].ToString();
                        user.DbPassword = ds.Tables[0].Rows[0][9].ToString();
                        user.ConnectionString = ds.Tables[0].Rows[0][10].ToString();
                    }
                    
                    //user.Status = this.GetString(0, reader);
                    //user.ErrorCode = this.GetString(1, reader);
                    //user.Message = this.GetString(2, reader);
                    //user.Locale = (int)this.GetInt(3, reader);
                    //user.ApplicationName = this.GetString(4, reader);
                    //user.MenuId = this.GetString(5, reader);
                    //user.ServerName = this.GetString(6, reader);
                    //user.DbName = this.GetString(7, reader);
                    //user.DbUser = this.GetString(8, reader);
                    //user.DbPassword = this.GetString(9, reader);
                    //user.ConnectionString = this.GetString(10, reader);
                }
            }
            return user;
        }
        public UserAuthenticate Logout(string userId, string appId, string device)
        {
            DataSet ds = new DataSet();
            UserAuthenticate user = null;
            using (DAL.Entities entities = new Entities("con"))
            {
                entities.Open();
                DbCommand cmd = entities.CreateCommand(SQL_PROCEDURE2);
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.Parameters.Add(entities.CreateParameter("@in_vchUserID", userId));
                cmd.Parameters.Add(entities.CreateParameter("@in_vchApplicationID", appId));
                cmd.Parameters.Add(entities.CreateParameter("@in_vchDevice", device));
                DbDataAdapter adapter = entities.CreateDataAdapter(cmd);
                adapter.Fill(ds);
                //string status = ds.Tables[0].Rows[0]["Status"].ToString();
                //string message = ds.Tables[0].Rows[0]["Message"].ToString(); 
                if (ds.Tables[0].Rows.Count > 0)
                {
                    user = new UserAuthenticate();
                    user.UserID = userId;
                    user.UserName = userId;
                    user.Status = ds.Tables[0].Rows[0][0].ToString();
                    user.ErrorCode = ds.Tables[0].Rows[0][1].ToString();
                    user.Message = ds.Tables[0].Rows[0][2].ToString();
                }
            }
            return user;
        }
        #endregion
    }
}
